package com.snow.springboot.service;

import com.alibaba.excel.context.AnalysisContext;
import com.snow.springboot.pojo.domain.CategoryExcelInfo;
import com.snow.springboot.pojo.domain.CategoryInfo;
import com.snow.springboot.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.snow.springboot.service
 * @ClassName: ExcelListenner
 * @Description:
 * @author: lurui
 * @Date: 2018/10/23 下午12:58
 * @version:
 */
@Component
public class ExcelListenner extends BaseListenner {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelListenner.class);

    @Autowired
    private CategoryService categoryService;
    @Value("${msgpath}")
    private String errorPath;

    @Override
    public void invoke(Object object, AnalysisContext context) {
        CategoryExcelInfo excelInfo = (CategoryExcelInfo) object;
        CategoryInfo categoryInfo = new CategoryInfo();
        BeanUtils.copyProperties(excelInfo, categoryInfo);
        categoryInfo.setCreateTime(new Date());
        boolean result = false;
        try {
            result = this.categoryService.saveCategorl(categoryInfo);
        } catch (SQLException e) {
            e.printStackTrace();
            LOGGER.error("save info error");
        }
        if (!result) {
            FileUtil.writeByte(errorPath, categoryInfo.getCategoryCode());
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public List getDatas() {
        return null;
    }
}
